﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_Status_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
    Dim level As String = -1
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            getStatusOtherList()
            getFunction()
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

        End If
    End Sub

    Protected Sub getStatusOtherList()
       
    End Sub

    Protected Sub getFunction()
        Using data As New VSEntities
            Dim p As List(Of SysFunction) = (From q In data.SysFunctions Where q.IsStatus = True Order By q.HrefName Ascending Select q).ToList
            With ddlFunction
                .DataSource = p
                .DataTextField = "HrefName"
                .DataValueField = "FunctionId"
                .DataBind()
            End With
            ddlFunction.Items.Insert(0, New ListItem("--- Chọn ---", "0"))
        End Using
    End Sub
    
    Protected Function checkExist() As Boolean
        Using data As New VSEntities
            Dim p = (From q In data.Status Where q.Title = txtStatusId.Text And q.FunctionId = ddlFunction.SelectedValue Select q).FirstOrDefault
            If p Is Nothing Then
                Return True
            Else
                Return False
            End If
        End Using
    End Function

    Protected Function Save() As Boolean
        Using data As New VSEntities
            Dim p As New VSModel.Status
            Try
                p.StatusId = txtStatusId.Text
                p.Title = txtTitle.Text.Trim()
                p.FunctionId = ddlFunction.SelectedValue
                data.Status.AddObject(p)
                data.SaveChanges()
                'Insert_App_Log("Insert  Status:" & txtTitle.Text.Trim & "", Function_Name.Status, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Return True
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Return False
            End Try
        End Using
    End Function
    Protected Sub ResetControl()
        txtTitle.Text = ""

        ddlFunction.SelectedValue = "0"
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If checkExist() Then
            If Save() Then
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Status/List.aspx';", Me.Page, True)
            Else
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End If
        Else
            Excute_Javascript("Alertbox('Đã tồn trạng thái này trên chức năng " + ddlFunction.SelectedItem.Text + ".');", Me.Page, True)
        End If
    End Sub
    
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Response.Redirect("List.aspx")
    End Sub
  
#End Region
End Class
